package com.scwatch.core.web.back;

import com.scwatch.core.domain.Site;
import com.scwatch.core.service.OperationLogService;
import com.scwatch.core.service.SiteService;
import com.scwatch.core.support.CmsException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.scwatch.core.constant.Constants.DELETE_SUCCESS;
import static com.scwatch.core.constant.Constants.MESSAGE;

/**
 * Created by rh005 on 2017/7/9.
 */
@Controller
@RequestMapping("/core/site_recycle")
public class SiteRecycleController {
    private static final Logger logger = LoggerFactory.getLogger(OperationLogController.class);

    @RequestMapping("list.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:site_recycle:list")
    public String list(@PageableDefault(sort = "treeNumber") Pageable pageable, HttpServletRequest request,
                       org.springframework.ui.Model modelMap,String name) {
        //Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
        //List<Site> list = service.findList(params, pageable.getSort());
        List<Site> list = service.findAllByStatus(1,name==null?"%%":"%"+name+"%");
        modelMap.addAttribute("list", list);
        modelMap.addAttribute("name", name);
        return "core/site/site_recycle";
    }

    @RequestMapping("delete.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:site_recycle:delete")
    public String delete(Integer[] ids, HttpServletRequest request, RedirectAttributes ra) {
        for (Integer id : ids) {
            // ID==1的默认站点不能删除
            if (id == 1) {
                throw new CmsException("site_recycle.error.defaultSiteCannotBeDeleted");
            }
        }
        Site[] beans = service.delete(ids);
        for (Site bean : beans) {
            logService.operation("opr.site.delete", bean.getName(), null, bean.getId(), request);
            logger.info("delete Site_recycle, name={}.", bean.getName());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
    }

    @RequestMapping("updateDeleteStatus.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:site_recycle:updateDeleteStatus")
    public String updateDeleteStatus(Integer[] ids, HttpServletRequest request, RedirectAttributes ra) {
        for (Integer id : ids) {
            // ID==1的默认站点不能删除
            if (id == 1) {
                throw new CmsException("site.error.defaultSiteCannotBeDeleted");
            }
        }
        Site[] beans = service.updateDeleteStatus(ids,0);
        for (Site bean : beans) {
            logService.operation("opr.site.delete", bean.getName(), null, bean.getId(), request);
            logger.info("updateDeleteStatus, name={}.", bean.getName());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
    }

    @Autowired
    private SiteService service;
    @Autowired
    private OperationLogService logService;
}